package com.fy.fyspace.controller;

import com.fy.fyspace.common.annotation.GlobalInterceptor;
import com.fy.fyspace.common.result.Result;
import com.fy.fyspace.constant.KeysConstant;
import com.fy.fyspace.model.dto.picture.*;
import com.fy.fyspace.model.vo.user.UserSessionVO;
import com.fy.fyspace.service.ReviewService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 图片审核管理
 */
@Api(tags = "图片审核管理")
@RestController
@RequestMapping("/review")
@Slf4j
public class ReviewController {
        @Resource
        private ReviewService reviewService;

        /**
         * 获取图片统计数据
         */
        @GetMapping("/statistics")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "获取图片统计数据", notes = "获取待审核数量、今日已审核数量、违规数量、总图片数等统计信息")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "获取成功", response = Result.class),
                        @ApiResponse(code = 400, message = "获取失败", response = Result.class),
        })
        public Result getStatistics() {
                log.info("开始获取图片统计数据");
                return reviewService.getStatistics();
        }

        /**
         * 获取待审核图片列表
         */
        @GetMapping("/list")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "获取待审核图片列表", notes = "支持多条件筛选和分页查询")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "获取成功", response = Result.class),
                        @ApiResponse(code = 400, message = "获取失败", response = Result.class)
        })
        public Result getPictureList(
                        @ApiParam(value = "页码", example = "1") @RequestParam(required = false) Integer pageNo,
                        @ApiParam(value = "每页数量", example = "30") @RequestParam(required = false) Integer pageSize,
                        @ApiParam(value = "搜索关键词", example = "风景") @RequestParam(required = false) String searchText,
                        @ApiParam(value = "审核状态", example = "通过") @RequestParam(required = false) String status,
                        @ApiParam(value = "开始时间", example = "2025-01-16 22:38:54") @RequestParam(required = false) String startTime,
                        @ApiParam(value = "结束时间", example = "2025-01-18 00:41:00") @RequestParam(required = false) String endTime,
                        HttpSession httpSession) {
                log.info("获取图片列表: page={}, pageSize={}, searchText={}, status={}, startTime={}, endTime={}",
                                pageNo, pageSize, searchText, status, startTime, endTime);
                return reviewService.getPictureList(pageNo, pageSize, searchText, status, startTime, endTime,
                                httpSession);
        }

        /**
         * 批量审核通过图片
         */
        @PostMapping("/batch-approve")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "批量审核通过图片", notes = "批量将图片状态修改为审核通过")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "审核成功", response = Result.class),
                        @ApiResponse(code = 400, message = "审核失败", response = Result.class)
        })
        public Result batchApprovePictures(
                        @ApiParam(value = "批量审核请求", required = true) @RequestBody PictureBatchApproveDTO request) {
                log.info("批量审核通过图片: pictureIds={}", request.getPictureIds());
                return reviewService.batchApprovePictures(request);
        }

        /**
         * 审核通过单张图片
         */
        @PostMapping("/approve/{pictureId}")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "审核通过单张图片", notes = "将单张图片状态修改为审核通过")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "审核成功", response = Result.class),
                        @ApiResponse(code = 400, message = "审核失败", response = Result.class)
        })
        public Result approvePicture(
                        @ApiParam(value = "图片ID", required = true, example = "1") @PathVariable Long pictureId,
                        HttpSession httpSession) {
                log.info("审核通过单张图片: pictureId={}", pictureId);
                UserSessionVO userSessionVO = (UserSessionVO) httpSession.getAttribute(KeysConstant.LOGIN_SESSION_KEY);
                return reviewService.approvePicture(pictureId, userSessionVO.getId());
        }

        /**
         * 拒绝单张图片
         */
        @PostMapping("/reject/{pictureId}")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "拒绝单张图片", notes = "将单张图片状态修改为审核拒绝")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "操作成功", response = Result.class),
                        @ApiResponse(code = 400, message = "操作失败", response = Result.class)
        })
        public Result rejectPicture(
                        @ApiParam(value = "图片ID", required = true, example = "1") @PathVariable Long pictureId,
                        @ApiParam(value = "拒绝原因", required = true) @RequestBody PictureRejectDTO request) {
                log.info("拒绝单张图片: pictureId={}, reason={}, comment={}",
                                pictureId, request.getRejectType(), request.getReviewMessage());
                return reviewService.rejectPicture(pictureId, request);
        }

        /**
         * 批量拒绝图片
         */
        @PostMapping("/batch-reject")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "批量拒绝图片", notes = "批量将图片状态修改为审核拒绝")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "操作成功", response = Result.class),
                        @ApiResponse(code = 400, message = "操作失败", response = Result.class)
        })
        public Result batchRejectPictures(
                        @ApiParam(value = "批量拒绝请求", required = true) @RequestBody PictureBatchRejectDTO request) {
                log.info("批量拒绝图片: pictureIds={}, type={}, message={}",
                                request.getPictureIds(), request.getRejectType(), request.getReviewMessage());
                return reviewService.batchRejectPictures(request);
        }

        /**
         * 更新图片信息
         */
        @PostMapping("/del/{pictureId}")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "更新图片信息", notes = "更新图片的基本信息")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "更新成功", response = Result.class),
                        @ApiResponse(code = 400, message = "更新失败", response = Result.class),
        })
        public Result updatePicture(
                        @ApiParam(value = "图片ID", required = true, example = "1") @PathVariable Long pictureId,
                        @ApiParam(value = "更新信息", required = true) @RequestBody PictureUpdateDTO request) {
                log.info("更新图片信息: pictureId={}, request={}", pictureId, request);
                return reviewService.updatePicture(pictureId, request);
        }

        /**
         * 删除单张图片
         */
        @PostMapping("/update/{pictureId}")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "删除单张图片", notes = "删除单张图片(逻辑删除)")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "删除成功", response = Result.class),
                        @ApiResponse(code = 400, message = "删除失败", response = Result.class),
        })
        public Result deletePicture(
                        @ApiParam(value = "图片ID", required = true, example = "1") @PathVariable Long pictureId) {
                log.info("删除单张图片: pictureId={}", pictureId);
                return reviewService.deletePicture(pictureId);
        }

        /**
         * 批量删除图片
         */
        @PostMapping("/batch")
        @GlobalInterceptor(checkAdmin = true)
        @ApiOperation(value = "批量删除图片", notes = "批量删除多张图片(逻辑删除)")
        @ApiResponses({
                        @ApiResponse(code = 200, message = "删除成功", response = Result.class),
                        @ApiResponse(code = 400, message = "删除失败", response = Result.class),
        })
        public Result batchDeletePictures(
                        @ApiParam(value = "批量删除请求", required = true) @RequestBody PictureBatchDeleteDTO request) {
                log.info("批量删除图片: pictureIds={}", request.getPictureIds());
                return reviewService.batchDeletePictures(request);
        }
}
